package com.lauor.test.query;

import com.alibaba.fastjson.JSON;
import com.lauor.smpedr.param.OptEnum;
import com.lauor.smpedr.param.SqlArgMap;
import com.lauor.smpedr.param.SqlParam;
import com.lauor.smpedr.session.SqlSession;
import com.lauor.test.BaseSpringBootUser;
import com.lauor.test.entity.User;
import org.junit.Test;

import java.util.List;

/**
 * 查询正常测试
 */
public class NormalQueryUser extends BaseSpringBootUser {

    //按条件查询多条数据,排序
    @Test
    public void queryAllByCondition(){
        try {
            SqlSession sqlSession = sqlSessionFactory.openSession();
            SqlArgMap<Integer> sqlArgMap = new SqlArgMap<>(4);
            //身高170以上的
            sqlArgMap.put("height", OptEnum.GT, 170);
            SqlParam sqlParam = new SqlParam(1, 10);
            sqlParam.setOrderBy("create_time desc");

            List<User> userList = sqlSession.selectList(User.class, sqlArgMap, sqlParam);
            for (User user : userList) {
                System.out.println( String.format("queryAllByCondition, result:%s", JSON.toJSON(user)) );
            }
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }

    //按条件查询单条数据,排序
    @Test
    public void querySingularByCondition(){
        try {
            SqlSession sqlSession = sqlSessionFactory.openSession();
            SqlArgMap<Integer> sqlArgMap = new SqlArgMap<>(4);
            //身高170以上的
            sqlArgMap.put("height", OptEnum.GT, 170);
            SqlParam sqlParam = new SqlParam();
            sqlParam.setOrderBy("create_time desc");

            User user = sqlSession.selectOne(User.class, sqlArgMap, sqlParam);
            System.out.println( String.format("queryAllByCondition, result:%s", JSON.toJSON(user)) );
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
    //自定义sql查询

}